package com.receiver.location;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
/**
 * 地图的第一层图层
 */
public class FirstOverlay extends ItemizedOverlay<OverlayItem>{
	private ArrayList<OverlayItem> overlayItems = new ArrayList<OverlayItem>();
	LocationReceiverActivity locationReceiverActivity;
	Handler handler;
	Message msg;
	
	//第一个参数用于指定标记所使用的默认图片
	public FirstOverlay(Drawable defaultMarker,Context context){
		//必须调用父类的构造函数
		super(boundCenterBottom(defaultMarker));
		this.locationReceiverActivity = (LocationReceiverActivity)context;
		
		this.handler = locationReceiverActivity.handler;
		msg = handler.obtainMessage();
	}
	
	public void addOverlay(OverlayItem overlayItem){
		overlayItems.add(overlayItem);
		populate();		//增加item后必须调用:为item的绘制做准备
	}
	
	//为polulate()读取item服务
	@Override
	protected OverlayItem createItem(int i) {
		return overlayItems.get(i);
	}

	@Override
	public int size() {
		return overlayItems.size();
	}

	@Override
	protected boolean onTap(int index) {
		OverlayItem item = overlayItems.get(index);
		AlertDialog.Builder builder = new AlertDialog.Builder(locationReceiverActivity);
		builder.setTitle("求助点信息");
		builder.setMessage(item.getTitle());
		msg.arg1 = Integer.parseInt(item.getTitle());
		builder.setPositiveButton("接警完成", new PositiveButtonListener());
		builder.setNegativeButton("返回", null);
		Dialog dialog = builder.create();
		dialog.show();
		return super.onTap(index);
	}
	
	class PositiveButtonListener implements OnClickListener{

		@Override
		public void onClick(DialogInterface dialog, int which) {
			msg.what = locationReceiverActivity.REMOVE_MARK;
			handler.sendMessage(msg);
		}
	}
	
}
